package cn.itcast.usermanager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.usermanager.pojo.EasyUIResult;
import cn.itcast.usermanager.pojo.User;
import cn.itcast.usermanager.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
//	@RequestMapping("users")
//	public String toUser(){
//		return "users";
//	}
//	@RequestMapping("page/add")
//	public String toAddUser(){
//		return "user-add";
//	}
	/**
	 * 新增通用的页面跳转方法
	 * @return
	 */
	@RequestMapping("page/{pageName}")
	public String toPage(@PathVariable("pageName")String pageName){
		return pageName;
	}
/*	@RequestMapping("list")
	@ResponseBody
	public List<User> queryUserAll(){
		List<User> users = userService.queryUserAll();
		return users;
	}*/
	@RequestMapping("list")
	@ResponseBody
	public EasyUIResult queryByPage(@RequestParam("page") int page,@RequestParam("rows") int rows){
		EasyUIResult easyUIResult = userService.queryByPage(page,rows);
		return easyUIResult;
	}
	@RequestMapping("save")
	@ResponseBody
	public Map<String, Object> saveUser(User user){
		Map<String, Object> map = new HashMap<String, Object>();
		boolean b;
		try {
			b = userService.save(user);
			if(b){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			map.put("status", "500");
			e.printStackTrace();
		}
		return map;
	}
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> deleteUser(@RequestParam("ids") List<Object> ids ){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Boolean b = userService.delete(ids);
			if (b) {
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			map.put("status", "500");
			e.printStackTrace();
		}
		return map;
	}
	@RequestMapping("edit")
	@ResponseBody
	public Map<String, Object> updateUser(User user){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Boolean b = userService.update(user);
			if (b) {
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			map.put("status", "500");
			e.printStackTrace();
		}
		return map;
	}
	@RequestMapping("export/excel")
	public String exportExcel(Model model,@RequestParam("page") Integer pageNum,@RequestParam("rows") Integer pageSize){
		EasyUIResult easyUIResult = userService.queryByPage(pageNum, pageSize);
		@SuppressWarnings("unchecked")
		List<User> userList = (List<User>) easyUIResult.getRows();
		model.addAttribute("userList", userList);
		return "userExcelView";
	}
	
}
